package view.parameterpanels;

import java.awt.Component;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.scaling.IFacility;
import net.miginfocom.swing.MigLayout;
import view.AlphaTextField;
import view.CenterTabbedPane.EBenchmarkTab;
import view.EBenchmarkPanel;
import view.NumericalTextField;
import controller.CtrInformationPanel;

//#Neu
/**
 * To draw a parameter panel for input parameters of type facility parameters.
 * The user can add facilities and assign a name to each of them.
 * @author Sebastian Perlwitz
 *
 */
public class FacilityParameterPanel extends EBenchmarkPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * The text fields that allow the user to assign a facility name.
	 */
	private List<AlphaTextField> facilityNameTextFields;
	
	/**
	 * A button to add a facility.
	 */
	private JButton btnAddFacility;
	
	/**
	 * A button to remove a facility.
	 */
	private JButton btnRemoveFacility;
	
	/**
	 * A JPanel where the user can setup what is shown on the panel.
	 */
	private JPanel settings;
	
	/**
	 * The text fields of the parameter panels.
	 */
	private List<NumericalTextField> textFields;
	
	/**
	 * The most important part of a ParameterPanel. On this panel
	 * all important parameter information is drawn.
	 */
	private JPanel paramPanel;
	
	/**
	 * The parameter panels get stored in here.
	 */
	private List<JPanel> parameterPanels;
	
    /**
	 * To create a new {@code FacilityParameterPanel}.
	 * @param infoPanelCtr the controller of the information panel.
	 * @param facility the facility to be referenced.
	 */
	public FacilityParameterPanel(
			CtrInformationPanel infoPanelCtr,
			IFacility facility) {
		super(null, EBenchmarkTab.FACILITY_PARAMETERS);
		facilityNameTextFields = new ArrayList<AlphaTextField>();
		
		this.setName("Anlagen");
		this.setActive(true);
		init();
		drawSettings();
	}
	
	/**
	 * To set the panel's initial conditions.
	 */
	private void init() {
		settings = new JPanel();
		paramPanel = new JPanel();
		paramPanel.setLayout(new MigLayout());
		parameterPanels = new ArrayList<JPanel>();
		textFields = new ArrayList<NumericalTextField>();
		
		this.setLayout(new MigLayout());
		this.add(settings, "wrap");
		this.add(paramPanel);
		this.add(logo, "aligny top");
	}
	
	
	/**
	 * To draw the settings panel of this {@code FacilityParameterPanel}.
	 */
	private void drawSettings() {
		settings.add(new JLabel("Anzahl der Anlagen festlegen: "));
		btnAddFacility = new JButton("+");
		btnRemoveFacility = new JButton("-");
		
		settings.add(btnAddFacility);
		settings.add(btnRemoveFacility);
	}
	
	/**
	 * To add a facility parameter set to this panel.
	 * @param paramInfo the parameters' information:<br>
     * paramInfo[0] = parameter's name<br>
     * paramInfo[1] = parameter's key for identification<br>
     * paramInfo[2] = parameter's abbreviation<br>
     * paramInfo[3] = parameter's unit<br>
     * paramInfo[4] = parameter's initial value.
     * @param borderTitle the parameters' description.
	 * @param facilityKey the identifying key of the facility whose parameter 
	 * panel is created.
	 * @return The newly created text fields.
	 */
	public List<JTextField> addParameterSet(
			List<String[]> paramInfo,
			String borderTitle,
			String facilityKey) {
		
		List<JTextField> newTextFields = new ArrayList<JTextField>();
		JPanel parameterPanel = new JPanel();
    	parameterPanel.setLayout(new MigLayout("wrap 4"));
    
		// Add facility description/name text field.
    	parameterPanel.add(new JLabel("Name der Anlage"));
    	parameterPanel.add(new JLabel(""));
    	parameterPanel.add(new JLabel(""));
    	
    	AlphaTextField alphaTF = new AlphaTextField(facilityKey);
    	alphaTF.setText(borderTitle);    	
    	alphaTF.setCaretPosition(0);    	
    	parameterPanel.add(alphaTF);
    	facilityNameTextFields.add(alphaTF);
    	newTextFields.add(alphaTF);
    	
    	// Add standard parameter panels.
    	newTextFields.addAll(
        		addStandardParameterPanels(
        				parameterPanel, paramInfo, borderTitle));
    	
    	return newTextFields;
    }
	
	/**
	 * To add a parameter set to this panel.
	 * @param paramInfo the parameters' information:<br>
     * paramInfo[0] = parameter's name<br>
     * paramInfo[1] = parameter's key for identification<br>
     * paramInfo[2] = parameter's abbreviation<br>
     * paramInfo[3] = parameter's unit<br>
     * paramInfo[4] = parameter's initial value.
     * @param borderTitle the parameters' description.
	 * @return The newly created text fields.
	 */
	private List<JTextField> addStandardParameterPanels(
			JPanel parameterPanel,
			List<String[]> paramInfo,
			String borderTitle){
		
		List<JTextField> newTextFields = new ArrayList<JTextField>();
		parameterPanel.setBorder(BorderFactory
                .createTitledBorder(borderTitle));
		for(String[] param : paramInfo) {
        	parameterPanel.add(new JLabel(param[0]));
            parameterPanel.add(new JLabel(param[2]));
            parameterPanel.add(new JLabel(param[3]));                
            
            NumericalTextField valueTF = new NumericalTextField(param[1]);
            valueTF.setText(param[4]);           
            parameterPanel.add(valueTF);
            textFields.add(valueTF);  
            newTextFields.add(valueTF);            
        }
		parameterPanels.add(parameterPanel);
        paramPanel.add(parameterPanel, "wrap");
        return newTextFields;		
	}
	
	/**
	 * To remove a parameter set from this panel.
	 * @return The removed text fields.
	 */
	public List<JTextField> removeParameterPanel() {
		
		int panelsCount = parameterPanels.size() - 1;
		List<JTextField> removedTextFields = 
				new ArrayList<JTextField>();
		List<NumericalTextField> removedNumericalTextFields = 
				new ArrayList<NumericalTextField>();
		List<AlphaTextField> removedAlphaTextFields = 
				new ArrayList<AlphaTextField>();
		
		if(panelsCount > 0) {			
			JPanel panelToRemove = parameterPanels.get(panelsCount);
			paramPanel.remove(panelToRemove);
			for(Component component : panelToRemove.getComponents()) {
				if(component instanceof NumericalTextField) {
					removedNumericalTextFields.add((NumericalTextField) component);
					removedTextFields.add((JTextField) component);
				} else if (component instanceof AlphaTextField) {
					removedAlphaTextFields.add((AlphaTextField) component);
					removedTextFields.add((JTextField) component);
				}
			}
			textFields.removeAll(removedNumericalTextFields);
			facilityNameTextFields.removeAll(removedAlphaTextFields);
			parameterPanels.remove(panelsCount);
			this.revalidate();
		}
		return removedTextFields;
	}
	
	/**
	 * To set a new text for a bordered parameter panel.
	 * @param position the position of the parameter panel within the panel.
	 * @param text the new text.
	 */
	public void setBorderText(int position, String text) {
		parameterPanels.get(position).setBorder(BorderFactory
                .createTitledBorder(text));
		this.revalidate();
	}
	
	@Override
	public void update() {
				
		if(getErrorStatus()) {
			// To disable all numerical text fields because of wrong input.		
			for(NumericalTextField txtField : textFields) {
				if(!txtField.getErrorStatus()) {
					txtField.setEnabled(false);
				}
			}
			
			// To disable the buttons.
			btnAddFacility.setEnabled(false);
			btnRemoveFacility.setEnabled(false);
			
			// To disable the facility name fields.
			for(AlphaTextField txtField : facilityNameTextFields) {
				if(!txtField.getErrorStatus()) {
					txtField.setEnabled(false);
				}
			}
			
		} else {
			// To enable all disabled numerical text fields when wrong input 
			// was corrected.
			for(NumericalTextField txtField : textFields) {
				if(!txtField.isEnabled()) {
					txtField.setEnabled(true);
				}
			}
			
			// To enable the buttons after the input was corrected.
			if(!btnAddFacility.isEnabled()) {
				btnAddFacility.setEnabled(true);
				btnRemoveFacility.setEnabled(true);
			}
			
			// To enable all facility name text fields when wrong input was 
			// corrected
			for(AlphaTextField txtField : facilityNameTextFields) {
				if(!txtField.isEnabled()) {
					txtField.setEnabled(true);
				}
			}
		}
	}
	
	/**
	 * To get the text fields that describe the facility.
	 * @return The text fields.
	 */
	public List<AlphaTextField> getFacilityNameTextFields() {
		return facilityNameTextFields;
	}
	
	/**
	 * To get the "plus" button of this panel.
	 * @return The button.
	 */
	public JButton getAddFacilityButton() {
		return btnAddFacility;
	}
	
	/**
	 * To get the "subtract" button of this panel.
	 * @return The button.
	 */
	public JButton getRemoveFacilityButton() {
		return btnRemoveFacility;
	}
	
}
